//tag references
var lcinsnme = $("#lcinsnme");
var lcvehnme = $("#lcvehnme");
var lcokbutn = $("#lcokbutn");
var lctabcot = $("#lctabcot");

var btpaginf = $('#btpaginf');
var btpaginp = $('#btpaginp');
var btpaginn = $('#btpaginn');
var btpaginl = $('#btpaginl');

// model objects
var colsCotacao = [ //
function(data) {
	return data.id;
}, function(data) {
	return data.segurado.nome;
}, function(data) {
	return data.veiculo.modelo;
}, ];
var options = {
	rowCreator : function(op) {
		var tr = $("<tr></tr>");
		tr[0].cotacao = listaCotacoes[op.rowIndex];
		tr.click(function() {
			window.prevcotacao = tr[0].cotacao;
			centernav("cotacao/novacotacao");
		});
		return tr[0];
	}
};
var listaCotacoes = [];
var totalCotacoes = 0;
var filter = {// the Javascript version of FiltroCotacaoTO
	insuredName : '',
	vehicleModel : '',
	start : 0,
	offset : 10
};
function buscar() {
	CotacaoFacade.getCotacao(filter, {
		callback : function(ret) {
			listaCotacoes = ret.cotacoes;
			totalCotacoes = ret.numCot;
			lctabcot.html("");
			dwr.util.addRows(lctabcot[0], ret.cotacoes, colsCotacao, options);
		}
	});
}
// events
lcokbutn.click(function() {// click,focus,change,and so on
	filter.insuredName = lcinsnme.val();
	filter.vehicleModel = lcvehnme.val();
	filter.start = 0;
	buscar();
});
btpaginf.click(function() {
	if (filter.start != 0) {
		filter.start = 0;
		buscar();
	}
});
btpaginp.click(function() {
	if (filter.start > 0) {
		filter.start -= filter.offset;
		if (filter.start < 0)
			filter.start = 0;
		buscar();
	}
});
btpaginn.click(function() {
	if (totalCotacoes > filter.start + filter.offset) {
		filter.start += filter.offset;
		buscar();
	}
});
btpaginl.click(function() {
	if (totalCotacoes > filter.start + filter.offset) {
		var remainder = totalCotacoes % filter.offset;
		if (remainder)
			filter.start = totalCotacoes - remainder;
		else
			filter.start = totalCotacoes - filter.offset;
		buscar();
	}
});
// basic setup
$("button").button();

// consistency
if (window.prevcotacao)
	window.prevcotacao = null;